package cn.tedu.yushougeproduct.service.impl;

import cn.tedu.yushouge.commons.ex.ServiceException;
import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.commons.util.PageInfoToPageDataConverter;
import cn.tedu.yushouge.commons.web.ServiceCode;
import cn.tedu.yushougeproduct.mapper.CartSkuItemMapper;
import cn.tedu.yushougeproduct.mapper.ShoppingCartMapper;
import cn.tedu.yushougeproduct.mapper.SkuMapper;
import cn.tedu.yushougeproduct.pojo.entity.CartSkuItem;
import cn.tedu.yushougeproduct.pojo.entity.Picture;
import cn.tedu.yushougeproduct.pojo.param.CartSkuItemUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.param.ShoppingCartItemAddParam;
import cn.tedu.yushougeproduct.pojo.vo.*;
import cn.tedu.yushougeproduct.service.ICartSkuItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CartSkuItemServiceImpl implements ICartSkuItemService {

    @Autowired
    private CartSkuItemMapper cartSkuItemMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public void addNew(ShoppingCartItemAddParam shoppingCartItemAddParam) {
        log.debug("开始处理【添加购物车项】的业务，参数：{}", shoppingCartItemAddParam);

        Long skuId = shoppingCartItemAddParam.getSkuId();
        Long cartId = shoppingCartItemAddParam.getShoppingCartId();
        Integer quantity = shoppingCartItemAddParam.getQuantity();
        int defaultQuantity = 1;
        int resolvedQuantity = (quantity != null) ? quantity : defaultQuantity;

        ShoppingCartStandardVO cart = shoppingCartMapper.getStandardById(cartId);
        log.debug("购物车参数：{}", cart);
        // 获取商品信息
        SkuStandardVO product = skuMapper.getStandardById(skuId);
        log.debug("商品参数：{}", product);

        BigDecimal unitPrice;
        if (product.getDiscountPrice().compareTo(BigDecimal.ZERO) != 0) {
            unitPrice = product.getDiscountPrice();
        } else {
            unitPrice = product.getOriginalPrice();
        }

        BigDecimal totalPrice = unitPrice.multiply(BigDecimal.valueOf(resolvedQuantity));

        // 添加购物车项
        CartSkuItem shoppingCartItem = new CartSkuItem();
        shoppingCartItem.setShoppingCartId(cartId);
        shoppingCartItem.setSkuId(skuId);
        shoppingCartItem.setQuantity(resolvedQuantity);
        shoppingCartItem.setPrice(totalPrice);
        shoppingCartItem.setGmtCreate(LocalDateTime.now());
        cartSkuItemMapper.insert(shoppingCartItem);

        int result = shoppingCartMapper.updateCartTotalPrice(cartId);
        log.debug("将新的购物车项数据插入到数据库，完成！影响行数为:{}",result);
    }

    @Override
    public void deleteByID(Long id) {
        log.debug("开始处理【根据id删除购物车项】的业务，参数：{}", id);

        CartSkuItemStandardVO cartItem = cartSkuItemMapper.getStandardById(id);
        Long cartId = cartItem.getShoppingCartId();

        QueryWrapper<CartSkuItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = cartSkuItemMapper.deleteById(id);
        if (countById == 0) {
            String message = "删除购物车项失败，购物车项不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("从购物车项删除一条数据，完成！");

        int result = shoppingCartMapper.updateCartTotalPrice(cartId);
        log.debug("将新的购物车项数据插入到数据库，完成！影响行数为:{}",result);
    }

    @Override
    public void updateQuantityByID(Long id, CartSkuItemUpdateInfoParam cartSkuItemUpdateInfoParam) {
        log.debug("开始处理【根据id修改购物车项】的业务，参数：{},新数据：{}", id,cartSkuItemUpdateInfoParam);

        CartSkuItemStandardVO cartItem = cartSkuItemMapper.getStandardById(id);
        Long skuId = cartItem.getSkuId();

        SkuStandardVO product = skuMapper.getStandardById(skuId);
        log.debug("商品参数：{}", product);

        BigDecimal unitPrice;
        if (product.getDiscountPrice().compareTo(BigDecimal.ZERO) != 0) {
            unitPrice = product.getDiscountPrice();
        } else {
            unitPrice = product.getOriginalPrice();
        }

        CartSkuItem cartSkuItem = new CartSkuItem();
        BeanUtils.copyProperties(cartSkuItemUpdateInfoParam,cartSkuItem);
        cartSkuItem.setId(id);
        cartSkuItem.setPrice(unitPrice.multiply(BigDecimal.valueOf(cartSkuItemUpdateInfoParam.getQuantity())));
        cartSkuItem.setGmtModified(LocalDateTime.now());
        int row = cartSkuItemMapper.updateById(cartSkuItem);
        if (row != 1) {
            String message = "修改购物车项数量详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("将新的购物车项数量更新入到数据库，完成！");

        Long cartId = cartItem.getShoppingCartId();

        int result = shoppingCartMapper.updateCartTotalPrice(cartId);
        log.debug("将新的购物车项数据插入到数据库，完成！影响行数为:{}",result);
    }

    @Override
    public List<ShoppingCartItemListVO> listByCartId(Long cartId) {
        log.debug("开始处理【根据购物车ID查询购物车项】的业务，购物车id：{}", cartId);
        return cartSkuItemMapper.listByCartId(cartId);
    }


}
